/********************************************************************************
 * Copyright (c)2016-2020, ShenZhen WanJiaAn Technology CO.,LTD.
 * All Rights Reserved.
 *
 * File Name     : DevConfig.c
 * Author        : joki.zhu
 * Date          : 2020-02-23
 *
 * Record        :
 * 1.Date        : 2020-02-23
 *   Author      : joki.zhu
 *   Modification: Created file
 ********************************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>

#include "s2j.h"
#include "tag_generator.h"
#include "DevConfig.h"
#include "ProductManager.h"

#ifdef __HuaweiLite__
#include "app_common.h"
#include "config_management.h"
#include "Audio.h"
#include "System.h"
#include "Color2Grey.h"
#endif /* __HuaweiLite__ */

#define DEVCONFIG_MUTEX_INIT_LOCK(mutex) do { (void) pthread_mutex_init(&mutex, NULL); } while(0)
#define DEVCONFIG_MUTEX_LOCK(mutex) do { (void) pthread_mutex_lock(&mutex); } while(0)
#define DEVCONFIG_MUTEX_UNLOCK(mutex) do { (void) pthread_mutex_unlock(&mutex); } while(0)

static pthread_mutex_t gs_DevConfLock;
static APP_CONFIG *gs_DevConfigHandle = NULL;

int DevConfigSetDefault(void)
{
    /* recovery default wifi config */
    DevConfigSetWifi("", "", "");
    DevConfigSetWifiSleepMode(0);
    DevConfigSetWifiDns("199.29.29.29", "114.114.114.114");

    /* recovery default audio config */
    PLAY_VOLUME_CONFIG stPlayVol = {100, 100, 100, 60};
    DevConfigSetPlayVolume(stPlayVol);

    /* recovery default video config */
    DevConfigSetIrNightMode(2);

    /* recovery default device config */
    DevConfigSetPirSwitch(1);
    DevConfigSetLogLevel(3);

    /* recovery default timezone config */
    TIMEZONE_CONFIG stTimeZone;
    memset(&stTimeZone, 0, sizeof(stTimeZone));
    stTimeZone.bEnable = 1;
    stTimeZone.startTime = 0;
    stTimeZone.endTime = 0;
    stTimeZone.zoneSec = 8 * 3600;
    DevConfigSetTimeZone(stTimeZone);
    
    return 0;
}

static void *json_to_struct(cJSON* json_obj) {	
    /* create Student structure object */
    s2j_create_struct_obj(struct_app, APP_CONFIG);
    if (struct_app == NULL) {
        return NULL;
    }
    
    // factory
    s2j_struct_get_struct_element(struct_factory, struct_app, json_factory, json_obj, FACTORY_CONFIG, factory);
    if (PRODUCT_MODEL_B5 == ProductGetHandle()->ProductModel) {
        s2j_struct_get_basic_element_ex(struct_factory, json_factory, string, model, "B5");
        s2j_struct_get_basic_element_ex(struct_factory, json_factory, string, hardVer, "B5-MAIN-V10");
        s2j_struct_get_basic_element_ex(struct_factory, json_factory, string, softVer, "B5_CN_V1.03");
    } else {
        s2j_struct_get_basic_element_ex(struct_factory, json_factory, string, model, "B1");
        s2j_struct_get_basic_element_ex(struct_factory, json_factory, string, hardVer, "B1-MAIN-V131");
#ifdef YD_PROTOCOL
        s2j_struct_get_basic_element_ex(struct_factory, json_factory, string, softVer, "B1_MC_V1.01.110");
#else
        s2j_struct_get_basic_element_ex(struct_factory, json_factory, string, softVer, "");
#endif /* YD_PROTOCOL */
    }
    s2j_struct_get_basic_element_ex(struct_factory, json_factory, string, mcuVer, "");
    s2j_struct_get_basic_element_ex(struct_factory, json_factory, string, mcuCrc, "");
    s2j_struct_get_basic_element_ex(struct_factory, json_factory, string, packTime, "");
    s2j_struct_get_basic_element_ex(struct_factory, json_factory, string, uuid, "0000000000000000");
    s2j_struct_get_basic_element_ex(struct_factory, json_factory, string, cmei, "");
    s2j_struct_get_basic_element_ex(struct_factory, json_factory, string, secretKey, "");
    s2j_struct_get_basic_element_ex(struct_factory, json_factory, string, alexaid, "");
    
    // video
    s2j_struct_get_struct_element(struct_video, struct_app, json_video, json_obj, VIDEO_CONFIG, video);
    s2j_struct_get_basic_element_ex(struct_video, json_video, int, width, 0);
    s2j_struct_get_basic_element_ex(struct_video, json_video, int, height, 0);
    s2j_struct_get_basic_element_ex(struct_video, json_video, int, bitrate, 0);
    s2j_struct_get_basic_element_ex(struct_video, json_video, int, bFlip, 0);
    s2j_struct_get_basic_element_ex(struct_video, json_video, int, irNightMode, 2);
    
    // audio
    s2j_struct_get_struct_element(struct_audio, struct_app, json_audio, json_obj, AUDIO_CONFIG, audio);
    s2j_struct_get_struct_element_ex(struct_playvol, struct_audio, json_playvol, json_audio, PLAY_VOLUME_CONFIG, playvol);
    s2j_struct_get_basic_element_ex(struct_playvol, json_playvol, int, bellVol, 100);
    s2j_struct_get_basic_element_ex(struct_playvol, json_playvol, int, alarmVol, 100);
    s2j_struct_get_basic_element_ex(struct_playvol, json_playvol, int, netcfgVol, 100);
    s2j_struct_get_basic_element_ex(struct_playvol, json_playvol, int, talkbackVol, 60);
    
    // wifi
    s2j_struct_get_struct_element(struct_wifi, struct_app, json_wifi, json_obj, WIFI_CONFIG, wifi);
    s2j_struct_get_basic_element_ex(struct_wifi, json_wifi, string, ssid, "");
    s2j_struct_get_basic_element_ex(struct_wifi, json_wifi, string, password, "");
    s2j_struct_get_basic_element_ex(struct_wifi, json_wifi, string, encrypt, "");
    s2j_struct_get_basic_element_ex(struct_wifi, json_wifi, int, bSleepMode, 0);
    s2j_struct_get_basic_element_ex(struct_wifi, json_wifi, string, dns1, "199.29.29.29");
    s2j_struct_get_basic_element_ex(struct_wifi, json_wifi, string, dns2, "114.114.114.114");

    // device
    s2j_struct_get_struct_element(struct_device, struct_app, json_device, json_obj, DEVICE_CONFIG, device);
    s2j_struct_get_basic_element_ex(struct_device, json_device, int, logLevel, 3);
    s2j_struct_get_basic_element_ex(struct_device, json_device, int, bLed, 1);
    s2j_struct_get_basic_element_ex(struct_device, json_device, int, bPir, 1);
    s2j_struct_get_basic_element_ex(struct_device, json_device, int, pirLevel, 3);
    s2j_struct_get_basic_element_ex(struct_device, json_device, int, bAlarm, 1);
    s2j_struct_get_basic_element_ex(struct_device, json_device, int, bBell, 1);

    // timezone
    s2j_struct_get_struct_element(struct_timezone, struct_app, json_timezone, json_obj, TIMEZONE_CONFIG, timezone);
    s2j_struct_get_basic_element_ex(struct_timezone, json_timezone, int, bEnable, 1);
    s2j_struct_get_basic_element_ex(struct_timezone, json_timezone, int, startTime, 0);
    s2j_struct_get_basic_element_ex(struct_timezone, json_timezone, int, endTime, 0);
    s2j_struct_get_basic_element_ex(struct_timezone, json_timezone, int, zoneSec, 8 * 3600);

    // upgrade
    s2j_struct_get_struct_element(struct_upgrade, struct_app, json_upgrade, json_obj, UPGRADE_CONFIG, upgrade);
    s2j_struct_get_basic_element_ex(struct_upgrade, json_upgrade, string, downUrl, "");
    s2j_struct_get_basic_element_ex(struct_upgrade, json_upgrade, string, md5, "");
    s2j_struct_get_basic_element_ex(struct_upgrade, json_upgrade, int, pktSize, 0);
    s2j_struct_get_basic_element_ex(struct_upgrade, json_upgrade, int, state, 0);
    s2j_struct_get_basic_element_ex(struct_upgrade, json_upgrade, int, retryCnt, 0);
    
    return struct_app;
}

static cJSON *struct_to_json(void* struct_obj) {
    APP_CONFIG *struct_app = (APP_CONFIG *)struct_obj;

    /* create Student JSON object */
    s2j_create_json_obj(json_app);

    // factory
    s2j_json_set_struct_element(json_factory, json_app, struct_factory, struct_app, FACTORY_CONFIG, factory);
    s2j_json_set_basic_element(json_factory, struct_factory, string, model);
    s2j_json_set_basic_element(json_factory, struct_factory, string, hardVer);
    s2j_json_set_basic_element(json_factory, struct_factory, string, softVer);
    s2j_json_set_basic_element(json_factory, struct_factory, string, mcuVer);
    s2j_json_set_basic_element(json_factory, struct_factory, string, mcuCrc);
    s2j_json_set_basic_element(json_factory, struct_factory, string, packTime);
    s2j_json_set_basic_element(json_factory, struct_factory, string, uuid);
    s2j_json_set_basic_element(json_factory, struct_factory, string, cmei);
    s2j_json_set_basic_element(json_factory, struct_factory, string, secretKey);
    s2j_json_set_basic_element(json_factory, struct_factory, string, alexaid);

    // video
    s2j_json_set_struct_element(json_video, json_app, struct_video, struct_app, VIDEO_CONFIG, video);
    s2j_json_set_basic_element(json_video, struct_video, int, width);
    s2j_json_set_basic_element(json_video, struct_video, int, height);
    s2j_json_set_basic_element(json_video, struct_video, int, bitrate);
    s2j_json_set_basic_element(json_video, struct_video, int, bFlip);
    s2j_json_set_basic_element(json_video, struct_video, int, irNightMode);
    
    // audio
    s2j_json_set_struct_element(json_audio, json_app, struct_audio, struct_app, AUDIO_CONFIG, audio);
    s2j_json_set_struct_element(json_playvol, json_audio, struct_playvol, struct_audio, PLAY_VOLUME_CONFIG, playvol);
    s2j_json_set_basic_element(json_playvol, struct_playvol, int, bellVol);
    s2j_json_set_basic_element(json_playvol, struct_playvol, int, alarmVol);
    s2j_json_set_basic_element(json_playvol, struct_playvol, int, netcfgVol);
    s2j_json_set_basic_element(json_playvol, struct_playvol, int, talkbackVol);

    // wifi
    s2j_json_set_struct_element(json_wifi, json_app, struct_wifi, struct_app, WIFI_CONFIG, wifi);
    s2j_json_set_basic_element(json_wifi, struct_wifi, string, ssid);
    s2j_json_set_basic_element(json_wifi, struct_wifi, string, password);
    s2j_json_set_basic_element(json_wifi, struct_wifi, string, encrypt);
    s2j_json_set_basic_element(json_wifi, struct_wifi, int, bSleepMode);
    s2j_json_set_basic_element(json_wifi, struct_wifi, string, dns1);
    s2j_json_set_basic_element(json_wifi, struct_wifi, string, dns2);

    // device
    s2j_json_set_struct_element(json_device, json_app, struct_device, struct_app, DEVICE_CONFIG, device);
    s2j_json_set_basic_element(json_device, struct_device, int, logLevel);
    s2j_json_set_basic_element(json_device, struct_device, int, bLed);
    s2j_json_set_basic_element(json_device, struct_device, int, bPir);
    s2j_json_set_basic_element(json_device, struct_device, int, pirLevel);
    s2j_json_set_basic_element(json_device, struct_device, int, bAlarm);
    s2j_json_set_basic_element(json_device, struct_device, int, bBell);

    // timezone
    s2j_json_set_struct_element(json_timezone, json_app, struct_timezone, struct_app, TIMEZONE_CONFIG, timezone);
    s2j_json_set_basic_element(json_timezone, struct_timezone, int, bEnable);
    s2j_json_set_basic_element(json_timezone, struct_timezone, int, startTime);
    s2j_json_set_basic_element(json_timezone, struct_timezone, int, endTime);
    s2j_json_set_basic_element(json_timezone, struct_timezone, int, zoneSec);

    // upgrade
    s2j_json_set_struct_element(json_upgrade, json_app, struct_upgrade, struct_app, UPGRADE_CONFIG, upgrade);
    s2j_json_set_basic_element(json_upgrade, struct_upgrade, string, downUrl);
    s2j_json_set_basic_element(json_upgrade, struct_upgrade, string, md5);
    s2j_json_set_basic_element(json_upgrade, struct_upgrade, int, pktSize);
    s2j_json_set_basic_element(json_upgrade, struct_upgrade, int, state);
    s2j_json_set_basic_element(json_upgrade, struct_upgrade, int, retryCnt);
    
    /* return Student JSON object pointer */
    return json_app;
}

static int print_buf2json(char *name, char *buf)
{
	if (name == NULL || buf == NULL) {
		printf("param is null\n");
		return -1;
	}

    cJSON *json_app = NULL;
    
	json_app = cJSON_Parse(buf);
	if (json_app == NULL) {
		printf("parse json is null\n");
		return -1;
	}
	printf("%s:\n%s\n", name, cJSON_Print(json_app));

    s2j_delete_json_obj(json_app);
    
    return 0;
}

int DevConfigGetAppConfig(APP_CONFIG *pstAppConfig)
{
	if (pstAppConfig == NULL) {
		printf("param is null\n");
		return -1;
	}

    int ret = 0;
    char bReadBak = 0;
	char configBuf[4096] = {0};
	cJSON *json_app = NULL;
	APP_CONFIG *struct_app = NULL;

	/* read json string from config file */
	memset(configBuf, 0, sizeof(configBuf));
	ret = TagGetValue(TAG_FILE_PATH, TAG_SEGMENT_TYPE_USER0, configBuf, sizeof(configBuf));
	if (ret < 0) {
        TagUpdate(TAG_FILE_PATH, NULL, NULL, "{}"); //set default
        snprintf(configBuf, sizeof(configBuf), "%s", "{}");
	}
	//printf("app configBuf : %s\n", configBuf);

	/* json string to json obj */
	json_app = cJSON_Parse(configBuf);
	if (json_app == NULL) {
	    printf("parse user0 json is null\n");
		bReadBak = 1;
	}

	if (bReadBak)
	{
	    memset(configBuf, 0, sizeof(configBuf));
        TagGetValue(TAG_FILE_PATH, TAG_SEGMENT_TYPE_USER0_BAK, configBuf, sizeof(configBuf));
        json_app = cJSON_Parse(configBuf);
        if (json_app == NULL) {
            printf("parse user0 bak json is null\n");
            return -1;
        }
	}
	//printf("json_data:\n%s\n", cJSON_Print(json_app));
	
	/* json to struct */
	struct_app = json_to_struct(json_app);
	memcpy(pstAppConfig, struct_app, sizeof(APP_CONFIG));
    
	s2j_delete_json_obj(json_app);
    s2j_delete_struct_obj(struct_app);
	
	return 0;
}

int DevConfigSetAppConfig(APP_CONFIG *pstAppConfig)
{
	if (pstAppConfig == NULL) {
		printf("param is null\n");
		return -1;
	}
	
	int ret = 0;
	char *strData = NULL;
	cJSON *jsonObj = NULL;

	/* struct to json, and json to string */
	jsonObj = struct_to_json(pstAppConfig);	
	//strData = cJSON_Print(json_app);
	strData = cJSON_PrintUnformatted(jsonObj);

	/* write json string to config file */
	ret = TagUpdate(TAG_FILE_PATH, NULL, NULL, strData);
	if (ret < 0) {
		printf("TagUpdate failed\n");
        goto free_json;
	}

free_json:
    if (jsonObj != NULL) {
        s2j_delete_json_obj(jsonObj);
    }
	return ret;
}

int DevConfigInit(void)
{
	int ret = 0;
	static char bInit = 0;

	if (bInit == 0)
	{
		DEVCONFIG_MUTEX_INIT_LOCK(gs_DevConfLock);
		
		gs_DevConfigHandle = (APP_CONFIG *)malloc(sizeof(APP_CONFIG));
		if (gs_DevConfigHandle == NULL) {
			printf("malloc failed\n");
			return -1;
		}

		memset(gs_DevConfigHandle, 0, sizeof(APP_CONFIG));
		ret = DevConfigGetAppConfig(gs_DevConfigHandle);
		if (ret < 0) {
			printf("DevConfigGetAppConfig failed\n");
			free(gs_DevConfigHandle);
			gs_DevConfigHandle = NULL;
			return -1;
		}
		
		bInit = 1;
	}

	return 0;
}

int DevConfigDeinit(void)
{
	DEVCONFIG_MUTEX_LOCK(gs_DevConfLock);
	
	if (gs_DevConfigHandle != NULL) {
		free(gs_DevConfigHandle);
		gs_DevConfigHandle = NULL;
	}
	
	DEVCONFIG_MUTEX_UNLOCK(gs_DevConfLock);

	return 0;
}

APP_CONFIG *DevConfigGetHandle(void)
{
	return gs_DevConfigHandle;
}

int DevConfigSave(void)
{
	DEVCONFIG_MUTEX_LOCK(gs_DevConfLock);

	int ret = 0;
	APP_CONFIG *pstAppConfig = DevConfigGetHandle();

	if (pstAppConfig != NULL) {
		ret = DevConfigSetAppConfig(pstAppConfig);
		if (ret < 0) {
			printf("DevConfigSetAppConfig failed\n");
			DEVCONFIG_MUTEX_UNLOCK(gs_DevConfLock);
			return -1;
		}
	}

	DEVCONFIG_MUTEX_UNLOCK(gs_DevConfLock);

	return 0;
}

int DevConfigCommit(void)
{
    int ret = 0;
    
    ret = DevConfigSave();
    if (ret < 0) {
        printf("DevConfigSave  failed\n");
        return -1;
    }
    
    return 0;
}

int DevConfigGetUuid(char *pUuid, int maxLen)
{
    DEVCONFIG_MUTEX_LOCK(gs_DevConfLock);

    APP_CONFIG *pstAppConfig = DevConfigGetHandle();

    if (pstAppConfig != NULL) {
        snprintf(pUuid, maxLen, "%s", pstAppConfig->factory.uuid);	
    }

    DEVCONFIG_MUTEX_UNLOCK(gs_DevConfLock);

    return 0;
}

int DevConfigSetUuid(char *pUuid)
{
    DEVCONFIG_MUTEX_LOCK(gs_DevConfLock);

    APP_CONFIG *pstAppConfig = DevConfigGetHandle();

    if (pstAppConfig != NULL) {
        snprintf(pstAppConfig->factory.uuid, sizeof(pstAppConfig->factory.uuid), "%s", pUuid);
    }

    DEVCONFIG_MUTEX_UNLOCK(gs_DevConfLock);

    DevConfigCommit();

    return 0;
}

int DevConfigGetCmei(char *pCmei, int maxLen)
{
    DEVCONFIG_MUTEX_LOCK(gs_DevConfLock);

    APP_CONFIG *pstAppConfig = DevConfigGetHandle();

    if (pstAppConfig != NULL) {
        snprintf(pCmei, maxLen, "%s", pstAppConfig->factory.cmei);	
    }

    DEVCONFIG_MUTEX_UNLOCK(gs_DevConfLock);

    return 0;
}

int DevConfigSetCmei(char *pCmei)
{
    DEVCONFIG_MUTEX_LOCK(gs_DevConfLock);

    APP_CONFIG *pstAppConfig = DevConfigGetHandle();

    if (pstAppConfig != NULL) {
        snprintf(pstAppConfig->factory.cmei, sizeof(pstAppConfig->factory.cmei), "%s", pCmei);
    }

    DEVCONFIG_MUTEX_UNLOCK(gs_DevConfLock);

    DevConfigCommit();

    return 0;
}

int DevConfigGetSecretKey(char *pSecrteKey, int maxLen)
{
    DEVCONFIG_MUTEX_LOCK(gs_DevConfLock);

    APP_CONFIG *pstAppConfig = DevConfigGetHandle();

    if (pstAppConfig != NULL) {
        snprintf(pSecrteKey, maxLen, "%s", pstAppConfig->factory.secretKey);	
    }

    DEVCONFIG_MUTEX_UNLOCK(gs_DevConfLock);

    return 0;
}

int DevConfigSetSecretKey(char *pSecrteKey)
{
    DEVCONFIG_MUTEX_LOCK(gs_DevConfLock);

    APP_CONFIG *pstAppConfig = DevConfigGetHandle();

    if (pstAppConfig != NULL) {
        snprintf(pstAppConfig->factory.secretKey, sizeof(pstAppConfig->factory.secretKey), "%s", pSecrteKey);
    }

    DEVCONFIG_MUTEX_UNLOCK(gs_DevConfLock);

    DevConfigCommit();

    return 0;
}

int DevConfigGetAlexaid(char *pAlexaid, int maxLen)
{
    DEVCONFIG_MUTEX_LOCK(gs_DevConfLock);

    APP_CONFIG *pstAppConfig = DevConfigGetHandle();

    if (pstAppConfig != NULL) {
        snprintf(pAlexaid, maxLen, "%s", pstAppConfig->factory.alexaid);	
    }

    DEVCONFIG_MUTEX_UNLOCK(gs_DevConfLock);

    return 0;
}

int DevConfigSetAlexaid(char *pAlexaid)
{
    DEVCONFIG_MUTEX_LOCK(gs_DevConfLock);

    APP_CONFIG *pstAppConfig = DevConfigGetHandle();

    if (pstAppConfig != NULL) {
        snprintf(pstAppConfig->factory.alexaid, sizeof(pstAppConfig->factory.alexaid), "%s", pAlexaid);
    }

    DEVCONFIG_MUTEX_UNLOCK(gs_DevConfLock);

    DevConfigCommit();

    return 0;
}

int DevConfigGetHardVersion(char *pVer, int maxLen)
{
    DEVCONFIG_MUTEX_LOCK(gs_DevConfLock);

    APP_CONFIG *pstAppConfig = DevConfigGetHandle();

    if (pstAppConfig != NULL) {
        snprintf(pVer, maxLen, "%s", pstAppConfig->factory.hardVer);	
    }

    DEVCONFIG_MUTEX_UNLOCK(gs_DevConfLock);

    return 0;
}

int DevConfigSetHardVersion(char *pVer)
{
    DEVCONFIG_MUTEX_LOCK(gs_DevConfLock);

    APP_CONFIG *pstAppConfig = DevConfigGetHandle();

    if (pstAppConfig != NULL) {
        snprintf(pstAppConfig->factory.hardVer, sizeof(pstAppConfig->factory.hardVer), "%s", pVer);
    }

    DEVCONFIG_MUTEX_UNLOCK(gs_DevConfLock);

    DevConfigCommit();

    return 0;
}

int DevConfigGetSoftVersion(char *pVer, int maxLen)
{
    DEVCONFIG_MUTEX_LOCK(gs_DevConfLock);

    APP_CONFIG *pstAppConfig = DevConfigGetHandle();

    if (pstAppConfig != NULL) {
        snprintf(pVer, maxLen, "%s", pstAppConfig->factory.softVer);   
    }

    DEVCONFIG_MUTEX_UNLOCK(gs_DevConfLock);

    return 0;
}

int DevConfigSetSoftVersion(char *pVer)
{
    DEVCONFIG_MUTEX_LOCK(gs_DevConfLock);

    APP_CONFIG *pstAppConfig = DevConfigGetHandle();

    if (pstAppConfig != NULL) {
        snprintf(pstAppConfig->factory.softVer, sizeof(pstAppConfig->factory.softVer), "%s", pVer);
    }

    DEVCONFIG_MUTEX_UNLOCK(gs_DevConfLock);

    DevConfigCommit();

    return 0;
}

int DevConfigGetMcuVersion(char *pVer, int maxLen)
{
    DEVCONFIG_MUTEX_LOCK(gs_DevConfLock);

    APP_CONFIG *pstAppConfig = DevConfigGetHandle();

    if (pstAppConfig != NULL) {
        snprintf(pVer, maxLen, "%s", pstAppConfig->factory.mcuVer);	
    }

    DEVCONFIG_MUTEX_UNLOCK(gs_DevConfLock);

    return 0;
}

int DevConfigSetMcuVersion(char *pVer)
{
    DEVCONFIG_MUTEX_LOCK(gs_DevConfLock);

    APP_CONFIG *pstAppConfig = DevConfigGetHandle();

    if (pstAppConfig != NULL) {
        snprintf(pstAppConfig->factory.mcuVer, sizeof(pstAppConfig->factory.mcuVer), "%s", pVer);
    }

    DEVCONFIG_MUTEX_UNLOCK(gs_DevConfLock);

    DevConfigCommit();

    return 0;
}

int DevConfigGetMcuCrc(char *pCrc, int maxLen)
{
    DEVCONFIG_MUTEX_LOCK(gs_DevConfLock);

    APP_CONFIG *pstAppConfig = DevConfigGetHandle();

    if (pstAppConfig != NULL) {
        snprintf(pCrc, maxLen, "%s", pstAppConfig->factory.mcuCrc);	
    }

    DEVCONFIG_MUTEX_UNLOCK(gs_DevConfLock);

    return 0;
}

int DevConfigSetMcuCrc(char *pCrc)
{
    DEVCONFIG_MUTEX_LOCK(gs_DevConfLock);

    APP_CONFIG *pstAppConfig = DevConfigGetHandle();

    if (pstAppConfig != NULL) {
        snprintf(pstAppConfig->factory.mcuCrc, sizeof(pstAppConfig->factory.mcuCrc), "%s", pCrc);
    }

    DEVCONFIG_MUTEX_UNLOCK(gs_DevConfLock);

    DevConfigCommit();

    return 0;
}

int DevConfigGetPackTime(char *packTime, int maxLen)
{
    DEVCONFIG_MUTEX_LOCK(gs_DevConfLock);

    APP_CONFIG *pstAppConfig = DevConfigGetHandle();

    if (pstAppConfig != NULL) {
        snprintf(packTime, maxLen, "%s", pstAppConfig->factory.packTime);
    }

    DEVCONFIG_MUTEX_UNLOCK(gs_DevConfLock);

    return 0;
}

int DevConfigSetPackTime(char *packTime)
{
    DEVCONFIG_MUTEX_LOCK(gs_DevConfLock);

    APP_CONFIG *pstAppConfig = DevConfigGetHandle();

    if (pstAppConfig != NULL) {
        snprintf(pstAppConfig->factory.packTime, sizeof(pstAppConfig->factory.packTime), "%s", packTime);
    }

    DEVCONFIG_MUTEX_UNLOCK(gs_DevConfLock);

    DevConfigCommit();

    return 0;
}

int DevConfigGetIrNightMode(int *pMode)
{
    DEVCONFIG_MUTEX_LOCK(gs_DevConfLock);

    APP_CONFIG *pstAppConfig = DevConfigGetHandle();

    if (pstAppConfig != NULL) {
        *pMode = pstAppConfig->video.irNightMode;
    }

    DEVCONFIG_MUTEX_UNLOCK(gs_DevConfLock);

    return 0;
}

int DevConfigSetIrNightMode(int mode)
{
    DEVCONFIG_MUTEX_LOCK(gs_DevConfLock);

    int ret = 0;
    APP_CONFIG *pstAppConfig = NULL;
    IR_NIGHT_MODE_E enIrNightMode = IR_NIGHT_MODE_NONE;

    switch (mode)
    {
        case 0:
            enIrNightMode = IR_NIGHT_OFF;
            break;
        case 1:
            enIrNightMode = IR_NIGHT_ON;
            break;
        case 2:
            enIrNightMode = IR_NIGHT_AUTO;
            break;
        default:
            LOGW("DevConfigSetIrNightMode mode(%d) is invalid\n", mode);
            DEVCONFIG_MUTEX_UNLOCK(gs_DevConfLock);
            return -1;
    }
    
    ret = Color2GreySetIrNightMode(enIrNightMode);
    if (ret < 0) {
        LOGE("Color2GreySetIrNightMode failed\n");
        DEVCONFIG_MUTEX_UNLOCK(gs_DevConfLock);
        return -1;
    }
	
    pstAppConfig = DevConfigGetHandle();
    if (pstAppConfig != NULL) {
        pstAppConfig->video.irNightMode = mode;
    }

    DEVCONFIG_MUTEX_UNLOCK(gs_DevConfLock);

    DevConfigCommit();

    return 0;
}


int DevConfigGetVideoResolution(int *pWidth, int *pHeight, int *pBitrate)
{
    DEVCONFIG_MUTEX_LOCK(gs_DevConfLock);

    APP_CONFIG *pstAppConfig = DevConfigGetHandle();

    if (pstAppConfig != NULL) {
        *pWidth = pstAppConfig->video.width;
        *pHeight = pstAppConfig->video.height;
        *pBitrate = pstAppConfig->video.bitrate;
    }

    DEVCONFIG_MUTEX_UNLOCK(gs_DevConfLock);

    return 0;
}

int DevConfigSetVideoResolution(int width, int height, int bitrate)
{
    DEVCONFIG_MUTEX_LOCK(gs_DevConfLock);

    int ret = 0;
    APP_CONFIG *pstAppConfig = NULL;
  
    pstAppConfig = DevConfigGetHandle();
    if (pstAppConfig != NULL) {
        pstAppConfig->video.width = width;
        pstAppConfig->video.height = height;
        pstAppConfig->video.bitrate = bitrate;
    }

    DEVCONFIG_MUTEX_UNLOCK(gs_DevConfLock);

    DevConfigCommit();

    return 0;
}

int DevConfigGetPlayVolume(PLAY_VOLUME_CONFIG *pstPlayVol)
{
    DEVCONFIG_MUTEX_LOCK(gs_DevConfLock);

    APP_CONFIG *pstAppConfig = DevConfigGetHandle();

    if (pstAppConfig != NULL) {
        pstPlayVol->bellVol = pstAppConfig->audio.playvol.bellVol;
        pstPlayVol->alarmVol = pstAppConfig->audio.playvol.alarmVol;
        pstPlayVol->netcfgVol = pstAppConfig->audio.playvol.netcfgVol;
        pstPlayVol->talkbackVol = pstAppConfig->audio.playvol.talkbackVol;
    }

    DEVCONFIG_MUTEX_UNLOCK(gs_DevConfLock);

    return 0;
}

/* Tip: 支持多个播放音量设置,若不修改其他播放音量,需要将其其它设置为-1 */
int DevConfigSetPlayVolume(PLAY_VOLUME_CONFIG stPlayVol)
{
    DEVCONFIG_MUTEX_LOCK(gs_DevConfLock);

    int ret = 0;
    APP_CONFIG *pstAppConfig = NULL;

    pstAppConfig = DevConfigGetHandle();
    if (pstAppConfig != NULL) {
        LOGW("bellVol:%d alarmVol:%d netcfgVol:%d talkbackVol:%d\n", stPlayVol.bellVol, stPlayVol.alarmVol, stPlayVol.netcfgVol, stPlayVol.talkbackVol);
        if (stPlayVol.bellVol >= 0) {
            pstAppConfig->audio.playvol.bellVol = stPlayVol.bellVol;
        }
        
        if (stPlayVol.alarmVol >= 0) {
            pstAppConfig->audio.playvol.alarmVol = stPlayVol.alarmVol;
        }

        if (stPlayVol.netcfgVol >= 0) {
            pstAppConfig->audio.playvol.netcfgVol = stPlayVol.netcfgVol;
        }

        if (stPlayVol.talkbackVol >= 0) {
            pstAppConfig->audio.playvol.talkbackVol = stPlayVol.talkbackVol;
        }
    }

    DEVCONFIG_MUTEX_UNLOCK(gs_DevConfLock);

    DevConfigCommit();

    return 0;
}

int DevConfigGetWifi(char *ssid, char *password, char *encrypt)
{
    DEVCONFIG_MUTEX_LOCK(gs_DevConfLock);

    APP_CONFIG *pstAppConfig = DevConfigGetHandle();

    if (pstAppConfig != NULL) {
        if (ssid != NULL) {
            snprintf(ssid, 32, "%s", pstAppConfig->wifi.ssid);	
        }

        if (password != NULL) {
            snprintf(password, 64, "%s", pstAppConfig->wifi.password);	
        }

        if (encrypt != NULL) {
            snprintf(encrypt, 32, "%s", pstAppConfig->wifi.encrypt);	
        }
    }

    DEVCONFIG_MUTEX_UNLOCK(gs_DevConfLock);

    return 0;
}

int DevConfigSetWifi(char *ssid, char *password, char *encrypt)
{
    DEVCONFIG_MUTEX_LOCK(gs_DevConfLock);

    APP_CONFIG *pstAppConfig = DevConfigGetHandle();

    if (pstAppConfig != NULL) {
        if (ssid != NULL) {
            snprintf(pstAppConfig->wifi.ssid, sizeof(pstAppConfig->wifi.ssid), "%s", ssid);
        }
        if (password != NULL) {
            snprintf(pstAppConfig->wifi.password, sizeof(pstAppConfig->wifi.password), "%s", password);
        }
        if (encrypt != NULL) {
            snprintf(pstAppConfig->wifi.encrypt, sizeof(pstAppConfig->wifi.encrypt), "%s", encrypt);
        }
    }
	
    DEVCONFIG_MUTEX_UNLOCK(gs_DevConfLock);

    DevConfigCommit();

    return 0;
}


int DevConfigGetWifiSleepMode(char *pMode)
{
    DEVCONFIG_MUTEX_LOCK(gs_DevConfLock);

    APP_CONFIG *pstAppConfig = DevConfigGetHandle();

    if (pstAppConfig != NULL) {
        *pMode = pstAppConfig->wifi.bSleepMode;
    }

    DEVCONFIG_MUTEX_UNLOCK(gs_DevConfLock);

    return 0;
}

int DevConfigSetWifiSleepMode(char mode)
{
    DEVCONFIG_MUTEX_LOCK(gs_DevConfLock);

    APP_CONFIG *pstAppConfig = NULL;

    pstAppConfig = DevConfigGetHandle();
    if (pstAppConfig != NULL) {
        pstAppConfig->wifi.bSleepMode = mode;
    }

    DEVCONFIG_MUTEX_UNLOCK(gs_DevConfLock);

    DevConfigCommit();

    return 0;
}

int DevConfigGetWifiDns(char *dns1, char *dns2)
{
    DEVCONFIG_MUTEX_LOCK(gs_DevConfLock);

    APP_CONFIG *pstAppConfig = DevConfigGetHandle();

    if (pstAppConfig != NULL) {
        if (dns1 != NULL) {
            snprintf(dns1, 16, "%s", pstAppConfig->wifi.dns1);
        }
        
        if (dns2 != NULL) {
            snprintf(dns2, 16, "%s", pstAppConfig->wifi.dns2);
        }
    }

    DEVCONFIG_MUTEX_UNLOCK(gs_DevConfLock);

    return 0;
}

int DevConfigSetWifiDns(char *dns1, char *dns2)
{
    DEVCONFIG_MUTEX_LOCK(gs_DevConfLock);

    APP_CONFIG *pstAppConfig = NULL;

    pstAppConfig = DevConfigGetHandle();
    if (pstAppConfig != NULL) {
        if (dns1 != NULL) {
            snprintf(pstAppConfig->wifi.dns1, 16, "%s", dns1);
        }
        
        if (dns2 != NULL) {
            snprintf(pstAppConfig->wifi.dns2, 16, "%s", dns2);
        }
    }

    DEVCONFIG_MUTEX_UNLOCK(gs_DevConfLock);

    DevConfigCommit();

    return 0;
}

int DevConfigGetLogLevel(int *pLogLevel)
{
    DEVCONFIG_MUTEX_LOCK(gs_DevConfLock);

    APP_CONFIG *pstAppConfig = DevConfigGetHandle();

    if (pstAppConfig != NULL) {
        *pLogLevel  = pstAppConfig->device.logLevel;
    }

    DEVCONFIG_MUTEX_UNLOCK(gs_DevConfLock);

    return 0;
}

int DevConfigSetLogLevel(int logLevel)
{
    DEVCONFIG_MUTEX_LOCK(gs_DevConfLock);

    APP_CONFIG *pstAppConfig = DevConfigGetHandle();

    if (pstAppConfig != NULL) {
        pstAppConfig->device.logLevel = logLevel;
    }

    DEVCONFIG_MUTEX_UNLOCK(gs_DevConfLock);

    DevConfigCommit();

    return 0;
}

int DevConfigGetPirSwitch(char *pPirSwitch)
{
    DEVCONFIG_MUTEX_LOCK(gs_DevConfLock);

    APP_CONFIG *pstAppConfig = DevConfigGetHandle();

    if (pstAppConfig != NULL) {
        *pPirSwitch  = pstAppConfig->device.bPir;
    }

    DEVCONFIG_MUTEX_UNLOCK(gs_DevConfLock);

    return 0;
}

int DevConfigSetPirSwitch(char pirSwitch)
{
    DEVCONFIG_MUTEX_LOCK(gs_DevConfLock);

    int ret = 0;
    APP_CONFIG *pstAppConfig = NULL;

    if (pirSwitch == 0 || pirSwitch == 1)
    {
        ret = SocSubSetPirSwitch(pirSwitch);
        if (ret < 0) {
            LOGE("SocSubSetPirSwitch failed\n");
            DEVCONFIG_MUTEX_UNLOCK(gs_DevConfLock);
            return -1;
        }
    }

    pstAppConfig = DevConfigGetHandle();
    if (pstAppConfig != NULL) {
        pstAppConfig->device.bPir = pirSwitch;
    }

    DEVCONFIG_MUTEX_UNLOCK(gs_DevConfLock);

    DevConfigCommit();

    return 0;
}

int DevConfigGetPirLevel(char *pPirLevel)
{
    DEVCONFIG_MUTEX_LOCK(gs_DevConfLock);

    APP_CONFIG *pstAppConfig = DevConfigGetHandle();

    if (pstAppConfig != NULL) {
        *pPirLevel  = pstAppConfig->device.pirLevel;
    }

    DEVCONFIG_MUTEX_UNLOCK(gs_DevConfLock);

    return 0;
}

int DevConfigSetPirLevel(char pirLevel)
{
    DEVCONFIG_MUTEX_LOCK(gs_DevConfLock);

    int ret = 0;
    int sensitivityValue = 0;
    APP_CONFIG *pstAppConfig = NULL;

    switch (pirLevel)
    {
        case 1:
            sensitivityValue = 100;
            break;
        case 2:
            sensitivityValue = 65;
            break;
        case 3:
            sensitivityValue = 35;
            break;
        default:
            LOGW("pirLevel %d is invalid\n", pirLevel);
            DEVCONFIG_MUTEX_UNLOCK(gs_DevConfLock);
            return -1;
    }
    ret = SocSubSetPirSensitivity(sensitivityValue);
    if (ret < 0) {
        LOGE("SocSubSetPirSensitivity failed\n");
        DEVCONFIG_MUTEX_UNLOCK(gs_DevConfLock);
        return -1;
    }

    pstAppConfig = DevConfigGetHandle();
    if (pstAppConfig != NULL) {
        pstAppConfig->device.pirLevel = pirLevel;
    }

    DEVCONFIG_MUTEX_UNLOCK(gs_DevConfLock);

    DevConfigCommit();

    return 0;
}

int DevConfigGetAlarmSwitch(char *pAlarmSwitch)
{
    DEVCONFIG_MUTEX_LOCK(gs_DevConfLock);

    APP_CONFIG *pstAppConfig = DevConfigGetHandle();

    if (pstAppConfig != NULL) {
        *pAlarmSwitch  = pstAppConfig->device.bAlarm;
    }

    DEVCONFIG_MUTEX_UNLOCK(gs_DevConfLock);

    return 0;
}

int DevConfigSetAlarmSwitch(char alarmSwitch)
{
    DEVCONFIG_MUTEX_LOCK(gs_DevConfLock);

    APP_CONFIG *pstAppConfig = DevConfigGetHandle();

    if (pstAppConfig != NULL) {
        pstAppConfig->device.bAlarm = alarmSwitch;
    }

    DEVCONFIG_MUTEX_UNLOCK(gs_DevConfLock);

    DevConfigCommit();

    return 0;
}

int DevConfigGetTimeZone(TIMEZONE_CONFIG *pstTimeZone)
{
    DEVCONFIG_MUTEX_LOCK(gs_DevConfLock);

    APP_CONFIG *pstAppConfig = DevConfigGetHandle();

    if (pstAppConfig != NULL) {
        memcpy(pstTimeZone, &pstAppConfig->timezone, sizeof(TIMEZONE_CONFIG));
    }

    DEVCONFIG_MUTEX_UNLOCK(gs_DevConfLock);

    return 0;
}

int DevConfigSetTimeZone(TIMEZONE_CONFIG stTimeZone)
{
    DEVCONFIG_MUTEX_LOCK(gs_DevConfLock);

    int ret = 0;
    SYS_TIMEZONE_INFO_S stSysTimeZone;
    APP_CONFIG *pstAppConfig = NULL;
    
    memset(&stSysTimeZone, 0, sizeof(stSysTimeZone));
    stSysTimeZone.zoneSec = stTimeZone.zoneSec;
    stSysTimeZone.stDayLightTime.bEnable = stTimeZone.bEnable;
    stSysTimeZone.stDayLightTime.startTime = stTimeZone.startTime;
    stSysTimeZone.stDayLightTime.endTime = stTimeZone.endTime;
    ret = SystemSetTimeZone(stSysTimeZone);
    if (ret < 0) {
        LOGE("SystemSetTimeZone failed\n");
        DEVCONFIG_MUTEX_UNLOCK(gs_DevConfLock);
        return -1;
    }
	
    pstAppConfig = DevConfigGetHandle();
    if (pstAppConfig != NULL) {
    memcpy(&pstAppConfig->timezone, &stTimeZone, sizeof(TIMEZONE_CONFIG));
    }

    DEVCONFIG_MUTEX_UNLOCK(gs_DevConfLock);

    DevConfigCommit();

    return 0;
}

int DevConfigGetUpgradeInfo(UPGRADE_CONFIG *pstUpgradeInfo)
{
    DEVCONFIG_MUTEX_LOCK(gs_DevConfLock);

    APP_CONFIG *pstAppConfig = DevConfigGetHandle();

    if (pstAppConfig != NULL) {
        snprintf(pstUpgradeInfo->downUrl, sizeof(pstUpgradeInfo->downUrl), "%s", pstAppConfig->upgrade.downUrl);
        snprintf(pstUpgradeInfo->md5, sizeof(pstUpgradeInfo->md5), "%s", pstAppConfig->upgrade.md5);
        pstUpgradeInfo->pktSize = pstAppConfig->upgrade.pktSize;
        pstUpgradeInfo->state = pstAppConfig->upgrade.state;
    }

    DEVCONFIG_MUTEX_UNLOCK(gs_DevConfLock);

    return 0;
}

int DevConfigSetUpgradeInfo(UPGRADE_CONFIG stUpgradeInfo)
{
    DEVCONFIG_MUTEX_LOCK(gs_DevConfLock);

    APP_CONFIG *pstAppConfig = NULL;

    pstAppConfig = DevConfigGetHandle();
    if (pstAppConfig != NULL) {
        snprintf(pstAppConfig->upgrade.downUrl, sizeof(pstAppConfig->upgrade.downUrl), "%s", stUpgradeInfo.downUrl);
        snprintf(pstAppConfig->upgrade.md5, sizeof(pstAppConfig->upgrade.downUrl), "%s", stUpgradeInfo.md5);
        pstAppConfig->upgrade.pktSize = stUpgradeInfo.pktSize;
        pstAppConfig->upgrade.state = stUpgradeInfo.state;
    }

    DEVCONFIG_MUTEX_UNLOCK(gs_DevConfLock);

    DevConfigCommit();

    return 0;
}

int DevConfigGetUpgradeState(int *pUpgradeState)
{
    DEVCONFIG_MUTEX_LOCK(gs_DevConfLock);

    APP_CONFIG *pstAppConfig = DevConfigGetHandle();

    if (pstAppConfig != NULL) {
        *pUpgradeState = pstAppConfig->upgrade.state;
    }

    DEVCONFIG_MUTEX_UNLOCK(gs_DevConfLock);

    return 0;
}

int DevConfigSetUpgradeState(int upgradeState)
{
    DEVCONFIG_MUTEX_LOCK(gs_DevConfLock);

    APP_CONFIG *pstAppConfig = NULL;

    pstAppConfig = DevConfigGetHandle();
    if (pstAppConfig != NULL) {
        pstAppConfig->upgrade.state = upgradeState;
    }

    DEVCONFIG_MUTEX_UNLOCK(gs_DevConfLock);

    DevConfigCommit();

    return 0;
}

int DevConfigGetUpgradeRetryCnt(int *pRetryCnt)
{
    DEVCONFIG_MUTEX_LOCK(gs_DevConfLock);

    APP_CONFIG *pstAppConfig = DevConfigGetHandle();

    if (pstAppConfig != NULL) {
        *pRetryCnt = pstAppConfig->upgrade.retryCnt;
    }

    DEVCONFIG_MUTEX_UNLOCK(gs_DevConfLock);

    return 0;
}

int DevConfigSetUpgradeRetryCnt(int retryCnt)
{
    DEVCONFIG_MUTEX_LOCK(gs_DevConfLock);

    APP_CONFIG *pstAppConfig = NULL;

    pstAppConfig = DevConfigGetHandle();
    if (pstAppConfig != NULL) {
        pstAppConfig->upgrade.retryCnt = retryCnt;
    }

    DEVCONFIG_MUTEX_UNLOCK(gs_DevConfLock);

    DevConfigCommit();

    return 0;
}

static void usage(const char *argv0)
{
	printf("Usage:\n");
	printf("    %s tag [create/update] [env_value] [boardinfo_value] [user0_value]\n", argv0);
	printf("    %s tag [default/recovery/dump]\n", argv0);
	printf("    %s user0 [format]\n", argv0);
	printf("    %s wifi [ssid] [password] [open/wpa+wpa2/wep]\n", argv0);
	printf("    %s uuid [value]\n", argv0);
	printf("    %s cmei [value]\n", argv0);
	printf("    %s secretkey [value]\n", argv0);
	printf("    %s version [value]\n", argv0);	
	printf("    %s packtime [value]\n", argv0);
	printf("    %s mcucrc [value]\n", argv0);
	printf("    %s log [value]\n", argv0);
	printf("    %s pir [value]\n", argv0);
	printf("    %s pirLevel [value]\n", argv0);
	printf("    %s playvol [value]\n", argv0);
	printf("    %s irnight [value]\n", argv0);
	printf("    %s timezone [enable] [startTime] [endTime] [zoneSec]\n", argv0);
	printf("    %s update [version]\n", argv0);
	printf("    %s updateinfo [value]\n", argv0);
	printf("    %s updatestate [value]\n", argv0);
	printf("    %s pickalarm [value]\n", argv0);
}

#ifdef __HuaweiLite__
int cmd_devtag(int argc, const char *argv[])
{
	int ret = 0;

	if (argc == 0) {
		usage("devtag");
		return -1;
	}

#if 0
	ret = DevConfigInit();
	if (ret < 0) {
		printf("DevConfigInit failed\n");
		return -1;
	}
#endif

	if (strcmp("tag", argv[0]) == 0)
	{
        if (argc == 1) {
            usage("devtag");
            return -1;
        }
        
	    if (strcmp("create", argv[1]) == 0) {
	        char env[1024] = "senv;[SW];version=V1.00;loglevel=3;init_vw=640;init_vh=360;[FAC];UUID=xxxx;[WIFI];SSID=xxxx;PASS=xxx;ENCRY=wpa+wpa2;eenv;";
	        char boardinfo[1024] = "sboard;[PRODUCT];MODLE=B1;CHIP=HI3518EV300;HWVER=B1-MAIN-V10;CLIENT=CN;[DRIVER];SENSORTYPE=PS5260;WIFITYPE=HI1131;BOOLAUDIO=1;BOOLMOTOR=1;BOOLUVC=1;BOOL4G=1;eboard";
	        TagCreate(TAG_FILE_PATH, env, boardinfo, NULL);
		} else if (strcmp("update", argv[1]) == 0) {
            TagUpdate(TAG_FILE_PATH, (char *)argv[2], (char *)argv[3], (char *)argv[4]);
		} else if (strcmp("default", argv[1]) == 0) {
				
		} else if (strcmp("recovery", argv[1]) == 0) {
		
		} else if (strcmp("dump", argv[1]) == 0) {
			TagDump(TAG_FILE_PATH);
        } else {
            usage("devtag");
            return -1;
        }
	}
	else if (strcmp("user0", argv[0]) == 0)
	{
	    if (argc == 1) {
            char buf[4096] = "";
            TagGetValue(TAG_FILE_PATH, TAG_SEGMENT_TYPE_USER0, buf, sizeof(buf));
            printf("user0:\n%s\n\n", buf);
            memset(buf, 0, sizeof(buf));
            TagGetValue(TAG_FILE_PATH, TAG_SEGMENT_TYPE_USER0_BAK, buf, sizeof(buf));
            printf("user0bak:\n%s\n\n", buf);
        } else if (argc == 2) {
            if (strcmp("format", argv[1]) == 0) {
                char buf[4096] = "";
                TagGetValue(TAG_FILE_PATH, TAG_SEGMENT_TYPE_USER0, buf, sizeof(buf));
                print_buf2json("user0", buf);
            }
        }
    }
	else if (strcmp("wifi", argv[0]) == 0)
	{
		if (argc == 1) {
			char ssid[32] = "";
			char password[64] = "";
			char encrypt[32] = "";

			DevConfigGetWifi(ssid, password, encrypt);
			printf("ssid: %s password: %s encrypt: %s\n", ssid, password, encrypt);
		} else if (argc == 4) {
			DevConfigSetWifi((char *)argv[1], (char *)argv[2], (char *)argv[3]);
		}
	}
	else if (strcmp("uuid", argv[0]) == 0)
	{
		if (argc == 1) {
			char uuid[32] = "";
			DevConfigGetUuid(uuid, sizeof(uuid));
			printf("uuid: %s\n", uuid);
		} else if (argc == 2) {
			DevConfigSetUuid((char *)argv[1]);
		}
	}
	else if (strcmp("cmei", argv[0]) == 0)
	{
		if (argc == 1) {
			char cmei[32] = "";
			DevConfigGetCmei(cmei, sizeof(cmei));
			printf("cmei: %s\n", cmei);
		} else if (argc == 2) {
			DevConfigSetCmei((char *)argv[1]);
		}
	}
	else if (strcmp("secretkey", argv[0]) == 0)
	{
		if (argc == 1) {
			char secretkey[128] = "";
			DevConfigGetSecretKey(secretkey, sizeof(secretkey));
			printf("secretkey: %s\n", secretkey);
		} else if (argc == 2) {
			DevConfigSetSecretKey((char *)argv[1]);
		}
	}	
	else if (strcmp("version", argv[0]) == 0)
	{
		if (argc == 1) {
			char version[32] = "";
			DevConfigGetSoftVersion(version, sizeof(version));
			printf("version: %s\n", version);
		} else if (argc == 2) {
			DevConfigSetSoftVersion((char *)argv[1]);
		}
	}
	else if (strcmp("packtime", argv[0]) == 0)
	{
		if (argc == 1) {
			char packTime[16] = "";
			DevConfigGetPackTime(packTime, sizeof(packTime));
			printf("packTime: %s\n", packTime);
		} else if (argc == 2) {
			DevConfigSetPackTime((char *)argv[1]);
		}
	}
	else if (strcmp("mcucrc", argv[0]) == 0)
	{
		if (argc == 1) {
			char mcuCrc[16] = "";
			DevConfigGetMcuCrc(mcuCrc, sizeof(mcuCrc));
			printf("mcuCrc: %s\n", mcuCrc);
		} else if (argc == 2) {
			DevConfigSetMcuCrc((char *)argv[1]);
		}
	}
	else if (strcmp("log", argv[0]) == 0)
	{
		if (argc == 1) {
			int logLevel = 0;
			DevConfigGetLogLevel(&logLevel);
			printf("logLevel: %d\n", logLevel);
		} else if (argc == 2) {
			DevConfigSetLogLevel(atoi(argv[1]));
		}
	}	
	else if (strcmp("pir", argv[0]) == 0)
	{
		if (argc == 1) {
			char pirSwitch = 0;
			DevConfigGetPirSwitch(&pirSwitch);
			printf("pirSwitch: %d\n", pirSwitch);
		} else if (argc == 2) {
			DevConfigSetPirSwitch(atoi(argv[1]));
		}
	}
	else if (strcmp("pirLevel", argv[0]) == 0)
	{
		if (argc == 1) {
			char pirLevel = 0;
			DevConfigGetPirLevel(&pirLevel);
			printf("pirLevel: %d\n", pirLevel);
		} else if (argc == 2) {
			DevConfigSetPirLevel(atoi(argv[1]));
		}
	}	
	else if (strcmp("playvol", argv[0]) == 0)
	{
		if (argc == 1) {
			PLAY_VOLUME_CONFIG stPlayVol;
			DevConfigGetPlayVolume(&stPlayVol);
			printf("alarmVol: %d\n", stPlayVol.alarmVol);
		} else if (argc == 2) {
		    PLAY_VOLUME_CONFIG stPlayVol = {-1, -1, -1, -1};
		    stPlayVol.alarmVol = atoi(argv[1]);
			DevConfigSetPlayVolume(stPlayVol);
		}
	}
	else if (strcmp("irnight", argv[0]) == 0)
	{
		if (argc == 1) {
			int irNightMode = 0;
			DevConfigGetIrNightMode(&irNightMode);
			printf("irNightMode: %d\n", irNightMode);
		} else if (argc == 2) {
			DevConfigSetIrNightMode(atoi(argv[1]));
		}
	}	
	else if (strcmp("timezone", argv[0]) == 0)
	{
		if (argc == 1) {
			TIMEZONE_CONFIG stTimeZone;
			memset(&stTimeZone, 0, sizeof(stTimeZone));
			DevConfigGetTimeZone(&stTimeZone);
			printf("enable:%d startTime:%d endTime:%d zoneSec:%d\n", stTimeZone.bEnable, 
			        stTimeZone.startTime, stTimeZone.endTime, stTimeZone.zoneSec);
		} else if (argc == 5) {
		    TIMEZONE_CONFIG stTimeZone;
		    memset(&stTimeZone, 0, sizeof(stTimeZone));
		    stTimeZone.bEnable = atoi(argv[1]);
		    stTimeZone.startTime = atoi(argv[2]);
		    stTimeZone.endTime = atoi(argv[3]);
		    stTimeZone.zoneSec = atoi(argv[4]);
			DevConfigSetTimeZone(stTimeZone);
		}
	}
	else if (strcmp("update", argv[0]) == 0)
	{
		if (argc == 2) {
		    DevConfigSetPackTime("1");
		    DevConfigSetMcuCrc("1");
			DevConfigSetSoftVersion((char *)argv[1]);
		}
	}
	else if (strcmp("updateinfo", argv[0]) == 0)
	{
		if (argc == 1) {
            UPGRADE_CONFIG stUpgradeInfo;
            memset(&stUpgradeInfo, 0, sizeof(stUpgradeInfo));
            DevConfigGetUpgradeInfo(&stUpgradeInfo);
            printf("downUrl: %s\n", stUpgradeInfo.downUrl);
            printf("md5: %s\n", stUpgradeInfo.md5);
            printf("pktSize: %d\n", stUpgradeInfo.pktSize);
		} else if (argc == 4) {
            UPGRADE_CONFIG stUpgradeInfo;
            memset(&stUpgradeInfo, 0, sizeof(stUpgradeInfo));
		    snprintf(stUpgradeInfo.downUrl, sizeof(stUpgradeInfo.downUrl), "%s", (char *)argv[1]);
		    snprintf(stUpgradeInfo.md5, sizeof(stUpgradeInfo.md5), "%s", (char *)argv[2]);
		    stUpgradeInfo.pktSize = atoi(argv[3]);
		    DevConfigSetUpgradeInfo(stUpgradeInfo);
		}
	}	
	else if (strcmp("updatestate", argv[0]) == 0)
	{
		if (argc == 1) {
			int updatestate = 0;
			DevConfigGetUpgradeState(&updatestate);
			printf("updatestate: %d\n", updatestate);
		} else if (argc == 2) {
			DevConfigSetUpgradeState(atoi(argv[1]));
		}
	}
	else if (strcmp("pickalarm", argv[0]) == 0)
	{
		if (argc == 1) {
			char pickAlarmSwitch = 0;
			DevConfigGetAlarmSwitch(&pickAlarmSwitch);
			printf("pickAlarmSwitch: %d\n", pickAlarmSwitch);
		} else if (argc == 2) {
			DevConfigSetAlarmSwitch(atoi(argv[1]));
		}
	}	
	else 
	{
		usage("devtag");
		return -1;
	}

#if 0
	ret = DevConfigDeinit();
	if (ret < 0) {
		printf("DevConfigDeinit faild\n");
		return -1;
	}
#endif

	return 0;
}

#else
int main(int argc, const char *argv[])
{
	int ret = 0;

	if (argc < 2) {
		usage(argv[0]);
		return -1;
	}

#if 1
    if (access(TAG_FILE_PATH, F_OK) != 0) {
		char env[1024] = "senv;[SW];version=V1.00;loglevel=3;init_vw=640;init_vh=360;[FAC];UUID=xxxx;[WIFI];SSID=xxxx;PASS=xxx;ENCRY=wpa+wpa2;eenv;";
		char boardinfo[1024] = "sboard;[PRODUCT];MODLE=B1;CHIP=HI3518EV300;HWVER=B1-MAIN-V10;CLIENT=CN;[DRIVER];SENSORTYPE=PS5260;WIFITYPE=HI1131;BOOLAUDIO=1;BOOLMOTOR=1;BOOLUVC=1;BOOL4G=1;eboard";
		TagCreate(TAG_FILE_PATH, env, boardinfo, "{}");
	}
#endif
	ret = DevConfigInit();
	if (ret < 0) {
		printf("DevConfigInit failed\n");
		return -1;
	}

	if (strcmp("tag", argv[1]) == 0)
	{
		if (argc == 2) {
			usage(argv[0]);
			return -1;
		}

	    if (strcmp("create", argv[2]) == 0) {
	        char env[1024] = "senv;[SW];version=V1.00;loglevel=3;init_vw=640;init_vh=360;[FAC];UUID=xxxx;[WIFI];SSID=xxxx;PASS=xxx;ENCRY=wpa+wpa2;eenv;";
	        char boardinfo[1024] = "sboard;[PRODUCT];MODLE=B1;CHIP=HI3518EV300;HWVER=B1-MAIN-V10;CLIENT=CN;[DRIVER];SENSORTYPE=PS5260;WIFITYPE=HI1131;BOOLAUDIO=1;BOOLMOTOR=1;BOOLUVC=1;BOOL4G=1;eboard";
	        TagCreate(TAG_FILE_PATH, env, boardinfo, NULL);
		} else if (strcmp("update", argv[2]) == 0) {
            TagUpdate(TAG_FILE_PATH, (char *)argv[3], (char *)argv[4], (char *)argv[5]);
		} else if (strcmp("default", argv[2]) == 0) {
				
		} else if (strcmp("recovery", argv[2]) == 0) {
		
		} else if (strcmp("dump", argv[2]) == 0) {
			TagDump(TAG_FILE_PATH);
        } else {
			usage(argv[0]);
			return -1;
		} 
	}
	else if (strcmp("user0", argv[1]) == 0)
	{
	    if (argc == 2) {
            char buf[4096] = "";
            TagGetValue(TAG_FILE_PATH, TAG_SEGMENT_TYPE_USER0, buf, sizeof(buf));
            printf("user0:\n%s\n\n", buf);
            memset(buf, 0, sizeof(buf));
            TagGetValue(TAG_FILE_PATH, TAG_SEGMENT_TYPE_USER0_BAK, buf, sizeof(buf));
            printf("user0bak:\n%s\n\n", buf);
	    } else if (argc == 3) {
            if (strcmp("format", argv[2]) == 0) {
                char buf[4096] = "";
                TagGetValue(TAG_FILE_PATH, TAG_SEGMENT_TYPE_USER0, buf, sizeof(buf));
                print_buf2json("user0", buf);
            }
	    }
	}
	else if (strcmp("wifi", argv[1]) == 0)
	{
		if (argc == 2) {
			char ssid[32] = "";
			char password[64] = "";
			char encrypt[32] = "";

			DevConfigGetWifi(ssid, password, encrypt);
			printf("ssid: %s password: %s encrypt: %s\n", ssid, password, encrypt);
		} else if (argc == 5) {
			DevConfigSetWifi((char *)argv[2], (char *)argv[3], (char *)argv[4]);
		}
	}
	else if (strcmp("uuid", argv[1]) == 0)
	{
		if (argc == 2) {
			char uuid[32] = "";
			DevConfigGetUuid(uuid, sizeof(uuid));
			printf("uuid: %s\n", uuid);
		} else if (argc == 3) {
			DevConfigSetUuid((char *)argv[2]);
		}
	}
	else if (strcmp("version", argv[1]) == 0)
	{
		if (argc == 2) {
			char version[32] = "";
			DevConfigGetSoftVersion(version, sizeof(version));
			printf("version: %s\n", version);
		} else if (argc == 3) {
			DevConfigSetSoftVersion((char *)argv[2]);
		}
	}
	else if (strcmp("packtime", argv[1]) == 0)
	{
		if (argc == 2) {
			char packTime[16] = "";
			DevConfigGetPackTime(packTime, sizeof(packTime));
			printf("packTime: %s\n", packTime);
		} else if (argc == 3) {
			DevConfigSetPackTime((char *)argv[2]);
		}
	}
	else if (strcmp("log", argv[1]) == 0)
	{
		if (argc == 2) {
			int logLevel = 0;
			DevConfigGetLogLevel(&logLevel);
			printf("logLevel: %d\n", logLevel);
		} else if (argc == 3) {
			DevConfigSetLogLevel(atoi(argv[2]));
		}
	}	
	else if (strcmp("pir", argv[1]) == 0)
	{
		if (argc == 2) {
			char pirSwitch = 0;
			DevConfigGetPirSwitch(&pirSwitch);
			printf("pirSwitch: %d\n", pirSwitch);
		} else if (argc == 3) {
			DevConfigSetPirSwitch(atoi(argv[2]));
		}
	}
	else if (strcmp("playvol", argv[1]) == 0)
	{
		if (argc == 2) {
			PLAY_VOLUME_CONFIG stPlayVol;
			DevConfigGetPlayVolume(&stPlayVol);
			printf("alarmVol: %d\n", stPlayVol.alarmVol);
		} else if (argc == 3) {
		    PLAY_VOLUME_CONFIG stPlayVol;
		    memset(&stPlayVol, -1, sizeof(stPlayVol));
		    stPlayVol.alarmVol = atoi(argv[2]);
			DevConfigSetPlayVolume(stPlayVol);
		}
	}
	else if (strcmp("timezone", argv[1]) == 0)
	{
		if (argc == 2) {
			TIMEZONE_CONFIG stTimeZone;
			memset(&stTimeZone, 0, sizeof(stTimeZone));
			DevConfigGetTimeZone(&stTimeZone);
			printf("enable:%d startTime:%d endTime:%d zoneSec:%d\n", stTimeZone.bEnable, 
			        stTimeZone.startTime, stTimeZone.endTime, stTimeZone.zoneSec);
		} else if (argc == 6) {
		    TIMEZONE_CONFIG stTimeZone;
		    memset(&stTimeZone, 0, sizeof(stTimeZone));
		    stTimeZone.bEnable = atoi(argv[2]);
		    stTimeZone.startTime = atoi(argv[3]);
		    stTimeZone.endTime = atoi(argv[4]);
		    stTimeZone.zoneSec = atoi(argv[5]);
			DevConfigSetTimeZone(stTimeZone);
		}
	}
	else 
	{
		usage(argv[0]);
		return -1;
	}

	ret = DevConfigDeinit();
	if (ret < 0) {
		printf("DevConfigDeinit faild\n");
		return -1;
	}

	return 0;
}
#endif /* __HuaweiLite__ */
